package com.sf.fy.uav.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.awt.JwtUtil;
import com.sf.fy.uav.common.enums.BillTypeEnum;
import com.sf.fy.uav.user.entity.RewardBill;
import com.sf.fy.uav.user.mapper.RewardBillMapper;
import com.sf.fy.uav.user.service.IRewardBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-08-24
 */
@Service
public class RewardBillServiceImpl extends ServiceImpl<RewardBillMapper, RewardBill> implements IRewardBillService {


    @Autowired
    private RewardBillMapper rewardBillMapper;

    @Override
    public BigDecimal getTotalDailyWithdrawal() {
        DateFormat fat = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<RewardBill> rewardBillQueryWrapper = new QueryWrapper<>();
        rewardBillQueryWrapper.eq("type", BillTypeEnum.WITHDRAWAL.getValue());
        rewardBillQueryWrapper.eq("date(create_time)", fat.format(new Date()));
        rewardBillQueryWrapper.select("sum(price_paid) as price_paid");
        RewardBill rewardBill = rewardBillMapper.selectOne(rewardBillQueryWrapper);
        if (ObjectUtils.isEmpty(rewardBill)) {
            return new BigDecimal(0);
        } else {
            return rewardBill.getPricePaid();
        }
    }

    @Override
    public BigDecimal getDailyWithdrawalByUserId(String userId) {
        DateFormat fat = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<RewardBill> rewardBillQueryWrapper = new QueryWrapper<>();
        rewardBillQueryWrapper.eq("type", BillTypeEnum.WITHDRAWAL.getValue());
        rewardBillQueryWrapper.eq("date(create_time)", fat.format(new Date()));
        rewardBillQueryWrapper.eq("user_id", userId);
        rewardBillQueryWrapper.select("sum(price_paid) as price_paid");
        RewardBill rewardBill = rewardBillMapper.selectOne(rewardBillQueryWrapper);
        if (ObjectUtils.isEmpty(rewardBill)) {
            return new BigDecimal(0);
        } else {
            return rewardBill.getPricePaid();
        }
    }
}
